from PyQt4 import QtCore, QtGui

# TODO: Unused; won't work unless we add threading.

from app import Application

class LoadingIcon(object):
    IMAGES_COUNT = 8
    images = []

    def __init__(self, parent, label):
        if not LoadingIcon.images:
            try:
                for i in range(LoadingIcon.IMAGES_COUNT):
                    filename = Application.image_path("loader", "%i.png" % i)
                    LoadingIcon.images.append(QtGui.QPixmap(filename))
            except:
                LoadingIcon.images = []

        self.label = label
        self.timer = QtCore.QTimer(parent)
        self.timer.timeout.connect(self.update)
        self.frame_number = 0       
        
    def start(self):
        if not LoadingIcon.images:
            return
        
        self.timer.start(100)
        self.frame_number = -1
        self.update()
        
    def stop(self):
        if not LoadingIcon.images:
            return
        
        self.timer.stop()
        self.label.clear()
        
    def update(self):
        if not LoadingIcon.images:
            return
        
        self.frame_number += 1
        if self.frame_number == LoadingIcon.IMAGES_COUNT:
            self.frame_number = 0
        
        self.label.setPixmap(LoadingIcon.images[self.frame_number])
